<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // function calcPrice(price, type) {
    //   if (type === '300_30') {
    //     price = price - Math.floor(price / 300) * 30
    //   } else if (type === '200_20') {
    //     price = price - Math.floor(price / 200) * 20
    //   } else if (type === '80%') {
    //     price = price * 0.8
    //   }

    //   return price
    // }
    // sale['300_30'](300)
    const calcPrice = (function () {
      const sale = {
        '300_30': (price) => (price - Math.floor(price / 300) * 30),
        '200_20': (price) => (price - Math.floor(price / 200) * 20),
        "80%": (price) => (price * 0.8)
      }
      const calcPrice = (type, price) => {
        if (!sale[type]) {
          console.log('暂无此优惠')
          return
        }
        return sale[type](price)
      }
      calcPrice.add = function (type, callback) {
        sale[type] = callback
      }
      calcPrice.remove = function (type) {
        delete sale[type]
      }
      return calcPrice
    })()

    console.log(calcPrice('300_30', 300))
    calcPrice.add('500_100', (price) => {
      return price - Math.floor(price / 500) * 100
    })
    console.log(calcPrice('500_100', 500))
    calcPrice.remove('500_100')
    console.log(calcPrice('500_100', 500))
  </script>
</body>

</html>